import sqlite3
import json
import sys

def examine_database():
    try:
        conn = sqlite3.connect('data/crawler.db')
        cursor = conn.cursor()

        # Get all table names
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
        tables = cursor.fetchall()
        print("Tables in database:")
        for table in tables:
            print(f"  - {table[0]}")

        # Examine each table structure
        for table in tables:
            table_name = table[0]
            print(f"\nStructure of '{table_name}' table:")
            cursor.execute(f"PRAGMA table_info({table_name});")
            columns = cursor.fetchall()
            for col in columns:
                print(f"  {col[1]} {col[2]} {'NOT NULL' if col[3] == 1 else 'NULL'}")

        # Sample data from each table
        for table in tables:
            table_name = table[0]
            print(f"\nSample data from '{table_name}' (first 3 rows):")
            try:
                cursor.execute(f"SELECT * FROM {table_name} LIMIT 3;")
                rows = cursor.fetchall()
                # Get column names
                cursor.execute(f"PRAGMA table_info({table_name});")
                columns = [col[1] for col in cursor.fetchall()]

                # Print header
                print("  " + " | ".join(columns))
                print("  " + "-" * 50)

                # Print data
                for row in rows:
                    print("  " + " | ".join(str(val) if val is not None else "NULL" for val in row))
            except Exception as e:
                print(f"  Error: {e}")

        conn.close()
    except Exception as e:
        print(f"Database error: {e}")

if __name__ == "__main__":
    examine_database()